Last Day Where You Can Still Cross
Try to solve the Last Day Where You Can Still Cross problem.
We'll cover the following
Statement#
You are given two integers, rows and cols, which represent the number of rows and columns in a 1-based binary matrix. In this matrix, each represents land, and each represents water.
Initially, on day 0, the whole matrix will just be all 0s, that is, all land. With each passing day, one of the cells of this matrix will get flooded and, therefore, will change to water, that is, from to . This continues until the entire matrix is flooded. You are given a 1-based array, water_cells, that records which cell will be flooded on each day. Each element indicates the cell present at the row and column of the matrix that will change from land to water on the day.
We can cross any cell of the matrix as long as it’s land. Once it changes to water, we can’t cross it. To cross any cell, we can only move in one of the four cardinal directions. Given the number of rows and columns of a 1-based binary matrix and a 1-based array, water_cells, you are required to find the last day where you can still cross the matrix, from top to bottom, by walking over the land cells only.
Note: You can start from any cell in the top row, and you need to be able to reach just one cell in the bottom row for it to count as a crossing.
Constraints:
-
rowscols -
rowscols water_cells.lengthrowscols-
rows -
cols - All values of
water_cellsare unique.
Examples#
1 of 6
2 of 6
3 of 6
4 of 6
5 of 6
6 of 6
Understand the problem#
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Last Day Where You Can Still Cross
What will be the last day where we can still cross the matrix, from top to bottom, if the following values are given as input?
5
On the 1st day, cell [1, 2] will be filled with water, and we can move from top to the bottom row. On the 2nd day, cell [1, 4] will be filled with water, and we can move from top to the bottom row. On the 3rd, 4th, and 5th days, cells [2, 1], [1, 1], and [2, 4] will be filled and we can still move from top to the bottom row. However, on the 6th day, cell [1, 3] will be filled with water and it is not possible to move from top to the bottom row. Therefore, 5 was the last day where we could still cross.
6
7
8
Figure it out!#
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself#
Implement your solution in main.py in the following coding playground. You’ll need the provided supporting code to implement your solution.
Solution: Longest Consecutive Sequence
Solution: Last Day Where You Can Still Cross